<?php

class UploadManager
{

    public static $_fileInfo;

    static protected function _preRequest($url, array $options = array())
    {
        if (!$url) {
            return 0;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/11.10 Chromium/18.0.1025.142 Chrome/18.0.1025.142 Safari/535.19');
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        return $ch;
    }

    static public function getRemoteFile($url, $proxy = null)
    {
        $options = array(
            CURLOPT_RETURNTRANSFER => 1
        );
        if ($proxy) {
            $options[CURLOPT_PROXY] = $proxy;
        }
        $ch = self::_preRequest($url, $options);

        $file = curl_exec($ch);
        self::$_fileInfo = curl_getinfo($ch);
        curl_close($ch);
        if (self::$_fileInfo['http_code'] != 200) {
            return false;
        }
        return $file;
    }

    static public function getRemoteFileStatus($url)
    {
        $ch = self::_preRequest($url, array(
                    CURLOPT_RETURNTRANSFER => 1
                ));
        curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $status;
    }

    static public function getRemoteFileInfo($url, $proxy = null)
    {
        if (!self::$_fileInfo) {
            $options = array(
                CURLOPT_RETURNTRANSFER => 1
            );
            if ($proxy) {
                $options[CURLOPT_PROXY] = $proxy;
            }

            $ch = self::_preRequest($url, $options);
            curl_exec($ch);
            self::$_fileInfo = curl_getinfo($ch);
            curl_close($ch);
        }
        return self::$_fileInfo;
    }
    static public function flushFileInfo()
    {
        self::$_fileInfo = null;
    }

}

?>